<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Calendar
 * @subpackage Event_Source_Abstract
 * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    $Id$
 */

/**
 * Zend_Calendar_Event_Source
 *
 * @category   Zend
 * @package    Zend_Calendar
 * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
abstract class Zend_Calendar_Event_Source_Abstract
{
	/**
	 * @var array Array of events for source.
	 */
	protected $_events = array();
	
	/**
	 * @var array Data for populating the events using source.
	 */
	protected $_data = array();
	
	/**
	 * @var array Params for defining individual properties of each source.
	 */
	protected $_params = array();
	
	/**
	 * @var int Default day for events in source.
	 */
	protected $_day;
	
	/**
	 * @var int Default month for events in source.
	 */
	protected $_month;
	
	/**
	 * @var int Default year for events in source.
	 */
	protected $_year;
	
	/**
	 * Class constructor.
	 * 
	 * @param array Data for source.
	 * @param array Optional parameters for source.
	 */
	public function __construct(array $data = array(), array $params = array())
	{
		$this->_day = date('d');
		$this->_month = date('m');
		$this->_year = date('Y');
		
		$this->setData($data);
		$this->setParams($params);
		
		$this->init();
	}
	
	/**
	 * Used by children classes to initialize source.
	 */
	public function init()
	{}
	
	/**
	 * @return array Source data.
	 */
	public function getData()
	{
		return $this->_data;
	}
	
	/**
	 * @param array Source data.
	 */
	public function setData($data)
	{
		$this->_data = $data;
		return $this;
	}
	
	/**
	 * @return int Default day.
	 */
	public function getDay()
	{
		return $this->_day;
	}
	
	/**
	 * @param int Default day.
	 */
	public function setDay($day)
	{
		$this->_day = $day;
		return $this;
	}
	
	/**
	 * @return int Default month.
	 */
	public function getMonth()
	{
		return $this->_month;
	}
	
	/**
	 * @param int Default month.
	 */
	public function setMonth($month)
	{
		$this->_month = $month;
		return $this;
	}
	
	/**
	 * @return int Default year.
	 */
	public function getYear()
	{
		return $this->_year;
	}
	
	/**
	 * @param int Default year.
	 */
	public function setYear($year)
	{
		$this->_year = $year;
		return $this;
	}
	
	/**
	 * Set the default parameters from array.
	 *  
	 * @param array Date array.
	 */
	public function setDate(array $date = array())
	{
		if (isset($date['day'])) {
			$this->setDay($date['day']);
		} else {
			$this->setDay(date('d'));
		}
		
		if (isset($date['month'])) {
			$this->setMonth($date['month']);
		} else {
			$this->setMonth(date('m'));
		}
		
		if (isset($date['year'])) {
			$this->setYear($date['year']);
		} else {
			$this->setYear(date('Y'));
		}
		return $this;
	}
	
	/**
	 * Returns parameter for event source.
	 * 
	 * @param string Parameter name.
	 * @return Parameter for given name or null if parameter doesn't exist.
	 */
	public function getParam($name)
	{
		if (isset($this->_params[$name])) {
			return $this->_params[$name];
		}
		
		return null;
	}
	
	/**
	 * Returns all parameters for event source.
	 * 
	 * @return array Event parameters.
	 */
	public function getParams()
	{
		return $this->_params;
	}
	
	/**
	 * @param array Event parameters.
	 */
	public function setParams(array $params = array())
	{
		$this->_params = $params;
		return $this;
	}
	
	/**
	 * Populates events using source, data, and parameters
	 * and returns them.
	 * 
	 * @return array Array of Zend_Calendar_Event_Source_*.
	 */
	public function getEvents()
	{
		if (0 == count($this->_events)) {
			$this->populateEvents();
		}
		
		return $this->_events;
	}
}